package com.codez.unionpay.springboot.mq;

import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author zhengdechao
 * @createTime : 2019-06-14
 *
 */

@Service
public class Sender {

	@Autowired
	AmqpTemplate template;
	
	public void test(String msg) {
		MessageProperties messageProperties = new MessageProperties();
		messageProperties.getHeaders().put("name", "codez");
		Message message = new Message(msg.getBytes(), messageProperties);
		template.convertAndSend("hello",message,new MessagePostProcessor() {
			
			@Override
			public Message postProcessMessage(Message message) throws AmqpException {
				message.getMessageProperties().getHeaders().put("name1", "codez1");
				return message;
			}
		});
	}
	
	public void sendDirect(String msg) {
		template.convertAndSend(RabbitConfig.DIRECT_QUEUE , msg);
	}
	
	public void sendTopic(String routingKey,String msg) {
		template.convertSendAndReceive(RabbitConfig.TOPIC_EXCHANGE, routingKey, msg);
	}
	
	public void sendFanout(String msg) {
		template.convertAndSend(RabbitConfig.FANOUT_EXCHANGE,"", msg);
	}
	
}
